#include"Gomoku.h"
#include"ProgressBar.h"
//玩家位置
int x;
int y;

int Menu()
{
  printf("\n\n");
  printf("##############################\n");
  printf("######    五子棋游      ######\n");
  printf("###  1.开始      0.退出   ####\n");
  printf("##############################\n");
  printf("##############################\n");
  printf("老铁,请输入您的选择:");
  int select=0;
  scanf("%d",&select);
  return select;
}

void ShowBoard(int board[ROW][COL])
{

  printf("\e[1;1H\e[2J");
  printf("  ");
  //显示第一行序列
  int i;
  for(i=1;i<=ROW;i++)
  {
      printf("%3d",i);
  }
  
  printf("\n");

  for(i=0;i<ROW;i++)
  {
    printf("%2d ",i+1);
    int j;
    for(j=0;j<COL;j++)
    {
      if(board[i][j]==0)
      {
          printf(" . ");
      }
      else if(board[i][j]==PLAYER1)
      {
        printf(" ● ");
      }
      else if(board[i][j]==PLAYER2)
      {
        printf(" ○ ");
      }
    
    }

      printf("\n");

  }

}

void PlayerMove(int board[ROW][COL],int CurPlayer)
{
     while(1)
     {
        printf("\nPlayer %d Input Pos(x y):",CurPlayer);
        scanf("%d %d",&x,&y);
        if(x<1||x>ROW||y<1||y>COL)
        {
           printf("输入范围越界请重新输入!\n");
            continue;
        }
        else if(board[x-1][y-1]!=0)
        {
            printf("铁子，你输入的位置已有归属，请重新选择位置！\n");
            continue;
        }
        else
        {
            board[x-1][y-1]=CurPlayer;
            break;
        }

     }

}

int ChessCount(int board[ROW][COL],enum Dir dir)
{
    
    int _x=x-1;
    int _y=y-1;
    int count=0; 
    while(1)
    {
         switch(dir)
         {
           case LEFT:
             _y--;
             break;
           case RIGHT:
             _y++;
             break;
           case UP:
             _x--;
             break;
           case DOWN:
             _x++;
             break;
           case LEFT_UP:
             _x--;
             _y--;
             break;
           case RIGHT_DOWN:
             _x++;
             _y++;
             break;
           case LEFT_DOWN:
             _x--;
             _y++;
             break;
           case RIGHT_UP:
             _x++;
             _y--;
             break;
         }
          
         //判断是否越界
          if(_x<0||_x>ROW-1||_y<0||_y>COL-1)
          {
              break;
          }
          //如果连珠则+1
          if(board[_x][_y]==board[x-1][y-1])
          {
                count++;
          }
       }
    return count;
}

int IsOver(int board[ROW][COL])
{
      int count1,count2,count3,count4;
      count1=ChessCount(board,LEFT) + ChessCount(board,RIGHT) + 1;
      count2=ChessCount(board,UP) + ChessCount(board,DOWN) + 1;
      count3=ChessCount(board,LEFT_UP) + ChessCount(board,RIGHT_DOWN) + 1;
      count4=ChessCount(board,LEFT_DOWN) + ChessCount(board,RIGHT_UP) + 1;

      //判断是否胜利
      if(count1>=5||count2>=5||count3>=5||count4>=5)
      {
            //判断是谁胜利
            if(board[x-1][y-1]==PLAYER1)
            {
                return PLAYER_WIN1;
            }

            return PLAYER_WIN2;
      }
      //判断是否可以继续
      int i;
      for(i=0;i<ROW;i++)
      {
        int j;
        for(j=0;j<COL;j++)
        {
              if(board[i][j]==0)
              {
                  return NEXT;
              }
        }
      }

      //走到这里时说明已经平局
      return DOGFALL;

}
void Game()
{
  
  ProgressBar();

  int board[ROW][COL]={0};
  int get=NEXT;
  do
  {
    ShowBoard(board);
    PlayerMove(board,PLAYER1);
    ShowBoard(board);
    get=IsOver(board);
    if(get!=NEXT)
    {
      break;
    }
    PlayerMove(board,PLAYER2);
    ShowBoard(board);
    get = IsOver(board);
    if(get!=NEXT)
    {
      break;
    }
  }while(1);
  
  switch(get)
  {
    case PLAYER_WIN1:
      printf("\n恭喜玩家 1 获得胜利！！！\n");
      break;
    case PLAYER_WIN2:
      printf("\n恭喜玩家 2 获得胜利！！！\n");
      break;
    case DOGFALL:
      printf("\n平局，和气生财！！！\n");
      break;
  }
}

